/**
 * 尊敬的用户，你好：页面 JS 面板是高阶用法，一般不建议普通用户使用，如需使用，请确定你具备研发背景，能够自我排查问题。
 * 我们可以用 JS 面板来开发一些定制度高功能，比如：调用API接口，自定义动作执行等等。
 */

// 当页面渲染完毕后马上调用下面的函数，这个函数是在当前页面 - 设置 - 生命周期 - 页面加载完成时中被关联的。
export function didMount() {
    console.log("「页面 JS」：当前页面地址 " + location.href);

    const echartsScriptTag = document.createElement('script');
    echartsScriptTag.src = 'https://cdn.jsdelivr.net/npm/echarts/dist/echarts.min.js';
    debugger
    const self = this;

    // 性别比例
    initSexDistribution(self);

    // 职位分布
    initPositionDistribution(self);

    // 年龄分布
    initAgeDistribution(self);

    // 学历分布
    initEducationDistribution(self);
}

/**
 * 学历分布
 * @param self
 */
function initEducationDistribution(self) {
    const educationDistribution = self.$$("text_fk9i6y").$el;
    let educationDistributionChart = echarts.init(educationDistribution);
    const eduLevels = ['大专及以下', '本科', '硕士', '博士'];
    const maleData = [35, 80, 40, 3];
    const femaleData = [25, 70, 40, 7];
    const educationOption = {
        title: {text: '学历分布（男女堆叠）', left: 'center'},
        tooltip: {trigger: 'axis', axisPointer: {type: 'shadow'}},
        legend: {data: ['男性', '女性'], top: 30},
        xAxis: {type: 'category', data: eduLevels},
        yAxis: {type: 'value', name: '人数'},
        series: [
            {
                name: '男性', type: 'bar', stack: 'gender',
                label: {show: true, position: 'inside'},
                itemStyle: {
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                        offset: 0,
                        color: '#3a8ee6'
                    }, {offset: 1, color: '#70c1ff'}])
                },
                data: maleData
            },
            {
                name: '女性', type: 'bar', stack: 'gender',
                label: {show: true, position: 'inside'},
                itemStyle: {
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                        offset: 0,
                        color: '#4cd964'
                    }, {offset: 1, color: '#a6f3c5'}])
                },
                data: femaleData
            }
        ]
    };

    educationDistributionChart.setOption(educationOption);
}

/**
 * 年龄分布
 * @param self
 */
function initAgeDistribution(self) {
    const ageDistribution = self.$$("text_be7a29").$el;
    let ageDistributionChart = echarts.init(ageDistribution);
    const ageGroups = ['<25岁', '25~30', '31~40', '41~50', '51~55', '56~60', '>60岁'];
    const male = [51, 56, 96, 83, 33, 5, 2];
    const female = [51, 63, 106, 90, 30, 15, 3];

    const ageDistributionOption = {
        title: {
            text: '各年龄段性别分布',
            left: 'center'
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {type: 'shadow'}
        },
        legend: {
            data: ['男性', '女性'],
            top: 30
        },
        grid: {
            left: 60,
            right: 40,
            bottom: 60,
            top: 80
        },
        xAxis: {
            type: 'category',
            data: ageGroups,
            axisTick: {alignWithLabel: true},
            axisLabel: {
                fontSize: 14
            }
        },
        yAxis: {
            type: 'value',
            name: '人数',
            nameTextStyle: {
                fontSize: 14
            },
            axisLabel: {
                fontSize: 14
            }
        },
        series: [
            {
                name: '男性',
                type: 'bar',
                stack: '性别',
                label: {
                    show: true,
                    position: 'inside'
                },
                itemStyle: {
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                        {offset: 0, color: '#3a8ee6'},
                        {offset: 1, color: '#70c1ff'}
                    ])
                },
                data: male
            },
            {
                name: '女性',
                type: 'bar',
                stack: '性别',
                label: {
                    show: true,
                    position: 'inside'
                },
                itemStyle: {
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                        {offset: 0, color: '#4cd964'},
                        {offset: 1, color: '#a6f3c5'}
                    ])
                },
                data: female
            }
        ]
    };

    ageDistributionChart.setOption(ageDistributionOption);
}


/**
 * 职位分布
 * @param self
 */
function initPositionDistribution(self) {
    const positionDistribution = self.$$("text_3sqke9").$el;
    let positionDistributionChart = echarts.init(positionDistribution);

    const ringData = [
        {name: "数据局", value: 1300, percentage: 13},
        {name: "数据处", value: 1200, percentage: 12},
        {name: "数据主中心", value: 1300, percentage: 13},
        {name: "数据主中中心", value: 1200, percentage: 12},
        {name: "数据中中心", value: 1200, percentage: 12},
        {name: "数据主中心", value: 1600, percentage: 16},
        {name: "数据主中中心", value: 1600, percentage: 16},
        {name: "数据次中心", value: 1600, percentage: 16}
    ];

    const ringOption = {
        graphic: {
            type: 'text',
            left: 'center',
            top: 'center',
            style: {
                text: '325\n总数',
                textAlign: 'center',
                fill: '#000',
                fontSize: 40,
                fontWeight: 'bold',
                lineHeight: 60
            }
        },
        legend: {
            orient: 'horizontal',
            bottom: 10,
            width: 400,
            data: ringData.map(item => item.name),
            itemWidth: 15,
            itemHeight: 15,
            itemGap: 10,
            formatter: (name) => {
                const item = ringData.find(d => d.name === name);
                return `{name|${name}}{value|${item.value}}{percentage|${item.percentage}%}`;
            },
            textStyle: {
                rich: {
                    name: {width: 80, align: 'left'},
                    value: {width: 40, align: 'right'},
                    percentage: {width: 40, align: 'right'}
                }
            }
        },
        series: [{
            type: 'pie',
            radius: ['40%', '55%'],
            center: ['50%', '50%'],
            data: ringData.map((item, index) => ({
                name: item.name,
                value: item.value,
                itemStyle: {color: ['#1E90FF', '#00CED1', '#20B2AA', '#3CB371', '#FF6347', '#FF4500', '#FFA500', '#FFD700'][index]}
            })),
            label: {show: false},
            emphasis: {label: {show: false}}
        }],
        tooltip: {trigger: 'item', formatter: '{b}: {c} ({d}%)'}
    };

    positionDistributionChart.setOption(ringOption);

}


/**
 * 性别比例
 * @param self
 */
function initSexDistribution(self) {
    const sexDistribution = self.$$("text_5oo0xw").$el;
    let sexDistributionChart = echarts.init(sexDistribution);
    const maleCount = 158;
    const femaleCount = 167;
    const totalCount = maleCount + femaleCount;

    const genderPieChart = {

        title: {text: '性别比例', left: 'center', top: 'center', textStyle: {fontSize: 16, color: '#666'}},
        series: [{
            type: 'pie',
            radius: ['40%', '50%'],
            avoidLabelOverlap: false,
            label: {show: false},
            data: [
                {value: maleCount, name: '男性', itemStyle: {color: '#3398DB'}},
                {value: femaleCount, name: '女性', itemStyle: {color: '#4cd964'}}
            ]
        }],
        legend: {
            orient: 'horizontal',
            bottom: 10,
            width: 400,
            data: [
                {
                    name: `男性 - ${maleCount} | ${((maleCount / totalCount) * 100).toFixed(1)}%`,
                    icon: 'circle'
                },
                {
                    name: `女性 - ${femaleCount} | ${((femaleCount / totalCount) * 100).toFixed(1)}%`,
                    icon: 'circle'
                }
            ],
            itemWidth: 15,
            itemHeight: 15,
            itemGap: 10
        }
    }

    sexDistributionChart.setOption(genderPieChart);
}
